package com.weasing.res.web.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Enumeration;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.weasing.res.utils.RESUtils;
import com.weasing.res.web.action.base.BaseAction;

@Controller
@Scope("prototype")
public class TestAction extends BaseAction<Object> {
	// 模板路径
	public static String relativelyPath = "C:\\Users\\long\\Desktop\\test.docx";
	private String destfilePath = RESUtils.getRequest()
			.getRealPath("//WEB-INF//evalRecord");
	// 通过模板生成临时文件返回文件格式文件
	public void getOutFile() {
		
		System.out.println(1111);
	}

	public void download() {
		String filePath = destfilePath + "//羁押必要性审查案件审批表.docx";
		File file = new File(filePath);
		HttpServletResponse response = RESUtils.getResponse();
		ServletOutputStream out = null;
		InputStream fin = null;
		try {
			// 调用工具类的createDoc方法生成Word文档
			// file = TestAction.getOutFile();
			fin = new FileInputStream(file);

			response.setCharacterEncoding("utf-8");
			response.setContentType("application/msword");
			// 设置浏览器以下载的方式处理该文件名


			response.setHeader("Content-Disposition", "attachment;filename="
					.concat(String
							.valueOf(URLEncoder.encode(file.getName(),
									"UTF-8"))));

			out = response.getOutputStream();
			byte[] buffer = new byte[512]; // 缓冲区
			int bytesToRead = -1;
			// 通过循环将读入的Word文件的内容输出到浏览器中
			while ((bytesToRead = fin.read(buffer)) != -1) {
				out.write(buffer, 0, bytesToRead);
			}
		} catch (Exception e) {
			System.out.println("error");
			e.printStackTrace();
		}

		finally {
			if (fin != null)
				try {
					fin.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			if (out != null)
				try {
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			if (file != null)
				file.delete(); // 删除临时文件
		}
	}



	/**
	 * 测试session里面有那些数据 清除不必要的
	 * 
	 * @author 黄龙
	 * 
	 */
	public void sessionTest() {
		HttpSession session = RESUtils.getSession();
		System.out.println(session);
		StringBuffer sb = new StringBuffer("");
		for (Enumeration e = session.getAttributeNames(); e
				.hasMoreElements();) {
			System.out.println(e.nextElement().toString());
			String s = e.nextElement().toString();
			sb.append("<p>" + s + "</p>\\\\");
				}
		RESUtils.print(sb.toString());
	}

	public void download2() {
		// 就用这个
		String filePath = destfilePath + "//羁押必要性审查案件审批表.docx";
		HttpServletResponse response = RESUtils.getResponse();
		ServletOutputStream out = null;
		InputStream fin = null;
		try {
			String filename = filePath.substring(filePath.lastIndexOf("/")+1);
			filename = new String(filename.getBytes("iso8859-1"),"UTF-8");

			String path = filePath;
			File file = new File(path);
			//如果文件不存在
			if(!file.exists()){
			    return ;
			}
			//设置响应头，控制浏览器下载该文件
			response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
			//读取要下载的文件，保存到文件输入流
			FileInputStream in = new FileInputStream(path);
			//创建输出流
			out = response.getOutputStream();
			//缓存区
			byte buffer[] = new byte[1024];
			int len = 0;
			//循环将输入流中的内容读取到缓冲区中
			while((len = in.read(buffer)) > 0){
			    out.write(buffer, 0, len);
			}
			//关闭
			in.close();
			out.close();     
			
			
		} catch (Exception e) {
		System.out.println("error");
		e.printStackTrace();
		}

	finally
	{
		if (fin != null)
			try {
				fin.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		if (out != null)
			try {
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}

	}
}